<?php

/**
* AdminAddSlideTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';
require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';
require_once '../framework/AuthSingleton.php';
require_once '../framework/Yt.class.php';


class  AdminAddSlideTest extends WebTestCase
{

    private $testName,$testDesc,$testGuideId,$url;
    private $dbh = null;
    private $username = '';
    private $password = '';

    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];
        $this->testName = md5(uniqid(rand(), true));
        $this->testDesc = md5(uniqid(rand(), true));
        $this->testGeocode = md5(uniqid(rand(), true));
        

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $this->dbh = new PDO($options['dsn_string']);        
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $this->dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName."','".$this->username."' )");
        $this->testGuideId =  $this->dbh->lastInsertId();

        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413"); 
        $this->get($this->url.'?cmd=AdminAddSlide&guide_id='.$this->testGuideId);

        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");

        $this->get($this->url.'?cmd=AdminAddSlide&guide_id='.$this->testGuideId);
    }
    public function testAddDataToDatabaseNoRequired()
    {
        
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("desc", $this->testDesc);
        $this->click("Zapisz");
        $this->assertText("Podaj nazwę");
    }

    public function testAddDataToDatabaseRequired()
    {
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->click("Zapisz");
        $this->assertText("Slajd dodany.");
    }
   
    public function testAddDataToDatabaseRequiredAndFoto()
    {
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
            

        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/images.jpg'));
        $this->assertFieldByName('pic_file', dirname(__FILE__).'/images.jpg');

        $this->click("Zapisz");

        $this->assertText("Slajd dodany.");
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = \"".$this->testName."\" 
                                                      AND desc = \"".$this->testDesc."\" 
                                                      AND pic_file  LIKE \"%images.jpg\"");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
    }

    public function testAddDataToDatabaseRequiredAndFotoWithWrongFileExtension()
    {
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
            

        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/fake.txt'));
        $this->assertFieldByName('pic_file', dirname(__FILE__).'/fake.txt');

        $this->click("Zapisz");

        $this->assertText("Niepoprawny format pliku");

    }

    public function testAddDataToDatabaseRequiredAndSound()
    {

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
            

        $this->assertTrue($this->setFieldByName('sound_file', dirname(__FILE__).'/sound.wav'));
        $this->assertFieldByName('sound_file', dirname(__FILE__).'/sound.wav');

        $this->click("Zapisz");

        $this->assertText("Slajd dodany.");
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $sth = $this->dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = \"".$this->testName."\" 
                                                      AND desc = \"".$this->testDesc."\" 
                                                      AND sound_file  LIKE \"%sound.wav\"");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
    }

    public function testAddDataToDatabaseRequiredAndSoundWithWrongFileExtension()
    {

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
            

        $this->assertTrue($this->setFieldByName('sound_file', dirname(__FILE__).'/fake.txt'));
        $this->assertFieldByName('sound_file', dirname(__FILE__).'/fake.txt');

        $this->click("Zapisz");

        $this->assertText("Niepoprawny format pliku");

    }

    public function testAddDataToDatabaseRequiredAndVideo()
    {

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
            

        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/video.avi'));
        $this->assertFieldByName('video_file', dirname(__FILE__).'/video.avi');

        $this->click("Zapisz");

        $this->assertText("Slajd dodany.");

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $sth = $this->dbh->prepare("SELECT video_file FROM slide WHERE name = \"".$this->testName."\" 
                                                      AND desc = \"".$this->testDesc."\" 
                                                      AND video_file NOT NULL");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch?hl=pl&v=".$result['video_file']);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        //wait a while to avoid google too many calls error
        sleep(1);

        $this->assertTrue(strpos($data,"Ten film wideo nie został jeszcze przetworzony."));


    }

    public function testAddDataToDatabaseRequiredAndVideoWithWrongFileExtension()
    {

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
            

        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/fake.txt'));
        $this->assertFieldByName('video_file', dirname(__FILE__).'/fake.txt');

        $this->click("Zapisz");

        $this->assertText("Niepoprawny format pliku");
    }
    public function testAddDataToDatabasAll()
    {

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->setFieldByName("geocode", $this->testGeocode);    

        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/images.jpg'));
        $this->assertFieldByName('pic_file', dirname(__FILE__).'/images.jpg');

        $this->assertTrue($this->setFieldByName('sound_file', dirname(__FILE__).'/sound.wav'));
        $this->assertFieldByName('sound_file', dirname(__FILE__).'/sound.wav');

        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/video.avi'));
        $this->assertFieldByName('video_file', dirname(__FILE__).'/video.avi');

        $this->click("Zapisz");

        $this->assertText("Slajd dodany.");
    }

    public function testFileSizeSet()
    {
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('MAX_FILE_SIZE',"2097152");
    }

    public function testNoRemoveButton()
    {
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertFalse( $this->click('Usuń') );
    }

    public function testOrderAddThreeSlidesAndChangeOrder()
    {
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('order', 0); 
        $this->setFieldByName("name", "0");
        $this->click("Zapisz");

        $this->get($this->url.'?cmd=AdminAddSlide&guide_id='.$this->testGuideId);
        $this->assertField('order',1);        
        $this->setFieldByName("name", "1");
        $this->click("Zapisz");

        $this->get($this->url.'?cmd=AdminAddSlide&guide_id='.$this->testGuideId);
        $this->assertField('order', 2);
        $this->setFieldByName("name", "2");
        $this->click("Zapisz");

        $this->get($this->url.'?cmd=AdminAddSlide&guide_id='.$this->testGuideId);
        $this->assertField('order', 3);
        $this->setFieldByName("name", "3");
        $this->setFieldByName("order",0);
        $this->click("Zapisz");

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');      
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sth = $this->dbh->prepare("SELECT name,`order` FROM slide WHERE guide_id = :guide_id ORDER BY `order` ASC");
        $sth->bindParam(':guide_id',$this->testGuideId,PDO::PARAM_INT);
        $sth->execute();
        $result = $sth->fetchAll(PDO::FETCH_BOTH);
    
        $row = $result['0'];
        $this->assertEqual($row['name'],3); 
        $this->assertEqual($row['order'],0); 

        $row = $result['1'];
        $this->assertEqual($row['name'],0); 
        $this->assertEqual($row['order'],1); 

        $row = $result['2'];
        $this->assertEqual($row['name'],1); 
        $this->assertEqual($row['order'],2); 

        $row = $result['3'];
        $this->assertEqual($row['name'],2); 
        $this->assertEqual($row['order'],3); 
        
    }

    public function testAddVideoFromIdAndFile()
    {
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        
        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/video.avi'));
        $this->assertFieldByName('video_file', dirname(__FILE__).'/video.avi');

        $this->setFieldByName("yt_id", "test");

        $this->click("Zapisz");
        $this->assertText("Podaj plik lub id");
    }
    
    public function testAddVideoWithWrongYtHref()
    {

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        

        $this->setFieldByName("yt_id", "test");

        $this->click("Zapisz");
        $this->assertText("Nieprawidłowy id");

    }

    public function testAddVideoWithYtHrefOk()
    {
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        

        $this->setFieldByName("yt_id", "jNQXAC9IVRw");

        $this->click("Zapisz");
        $this->assertText("Slajd dodany");
    }

    public function tearDown() 
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $sth = $this->dbh->query("SELECT pic_file,sound_file,video_file FROM slide WHERE guide_id=".$this->testGuideId);

        $result = $sth->fetch();
        if ( is_file($upload['upload_dir'].$result['pic_file']) )
        {
            unlink($upload['upload_dir'].$result['pic_file']);
        }

        if ( is_file($upload['upload_dir'].$result['sound_file']) )
        {
            unlink($upload['upload_dir'].$result['sound_file']);
        }

        if ( $result['video_file'] != '' )
        {
            $yt = Yt::getClient();
        
            try
            {
                $videoEntry = $yt->getVideoEntry($result['video_file'],null,true);
                $yt->delete($videoEntry);
            }
            catch(Zend_Gdata_App_HttpException $e)
            {
            }
        }

        $sth = $this->dbh->prepare("DELETE FROM slide WHERE guide_id=".$this->testGuideId);
        $sth -> execute();
        $sth = $this->dbh->prepare("DELETE FROM guide WHERE ROWID=".$this->testGuideId);
        $sth -> execute();

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminAddSlide&logout=1');
        
    }
    
}
?>
